let isVoiceControlEnabled;
const toggleButton = document.getElementById('toggleButton');
if(localStorage .getItem('isVoiceControlEnabled') == null 
|| localStorage .getItem('isVoiceControlEnabled') == undefined 
|| localStorage .getItem('isVoiceControlEnabled') == 'true'){
    isVoiceControlEnabled = true;
}else{
    localStorage .setItem('isVoiceControlEnabled', false);
    isVoiceControlEnabled = false;
}
if(isVoiceControlEnabled == true){
    toggleButton.textContent = '关闭语音控制';
}else{
    toggleButton.textContent = '开启语音控制';
}
const updateButton = () => {
    toggleButton.textContent = isVoiceControlEnabled ? '关闭语音控制' : '开启语音控制';
    localStorage .setItem('isVoiceControlEnabled', isVoiceControlEnabled);
};
const defaultCommands = {
    nextAudio: "下一个;换一个",
    proAudio: "上一个",
    openComments: "看评论;打开评论;评论区;打开评论区",
    like: "点赞",
    notInterested: "不感兴趣",
    viewAuthor: "看作者",
    collect: "收藏",
    share: "分享",
    playPause: "播放;暂停",
    fullScreenWeb: "网页全屏",
    fullScreen: "全屏"
};
let commands ;
if(localStorage .getItem('commands') == null || localStorage .getItem('commands') == undefined ){
    localStorage .setItem('commands', JSON.stringify(defaultCommands));
    commands = JSON.parse(localStorage .getItem('commands'));
}else{
    commands = JSON.parse(localStorage .getItem('commands'));
}
// 获取表单元素
const form = document.getElementById("command-list");
function initializeForm() {
    Object.keys(commands).forEach((key) => {
        const input = document.getElementById(key);
        if (input) {
            input.value = commands[key]; // 填充默认值
        }
    });
}
// 绑定事件监听器，更新 commands 对象
function bindFormEvents() {
    Object.keys(commands).forEach((key) => {
        const input = document.getElementById(key);
        if (input) {
            input.addEventListener("input", (event) => {
                commands[key] = event.target.value; // 更新 commands 对象
                localStorage .setItem('commands', JSON.stringify(commands));
                try {
                    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
                        chrome.tabs.sendMessage(tabs[0].id, { commands: commands }, (response) => {
                            if (response && response.status == 'success') {
                                // alert('消息成功发送到内容脚本');
                            } else {
                                // alert('未收到有效响应');
                            }
                        });
                    });
                } catch (e) {
                    alert(e);
                }
            });
        }
        
    });
}
document.addEventListener("DOMContentLoaded", () => {
    initializeForm();
    bindFormEvents();
});
// 点击按钮时的逻辑
toggleButton.addEventListener('click', () => {
    isVoiceControlEnabled = !isVoiceControlEnabled;
    updateButton();
    // 通知内容脚本更新状态
    try {
        chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
            chrome.tabs.sendMessage(tabs[0].id, { voiceControlEnabled: isVoiceControlEnabled }, (response) => {
                if (response && response.status == 'success') {
                    // alert('消息成功发送到内容脚本');
                } else {
                    // alert('未收到有效响应');
                }
            });
        });
    } catch (e) {
        alert(e);
    }
});